using System;
using System.Collections;
using Awaiter;
using Nsb.Classes;
using Theme;
using Yoshi.Classes;
using YoshiBase.Classes;
using jQueryApi;

namespace Yoshi.Clusters
{
    public class EmailProfile : EmailBase
    {
        private jQueryObject _jar;
        private jQueryObject _photo;
        private jQueryObject _image;
        private jQueryObject _info;
        private jQueryObject _name;
        private jQueryObject _bio;
        public static string OtherPersonId;

        public EmailProfile(EmailCluster cluster) : base(cluster)
        {
        }

        public static void SetOther(string id)
        {
            OtherPersonId = id;
        }

        public override EmailBase Build(bool active)
        {
            Initialize(active);

            Element.AddClass("Section");

            _jar = jQuery.FromHtml("<div class='Jar'/>")
                .AppendTo(Element)
                .Hide()
                ;

            _photo = jQuery.FromHtml("<div class='PhotoCell'/>")
                .AppendTo(_jar)
                ;

            _image = jQuery.FromHtml("<img/>")
                .AppendTo(_photo)
                .CSS(
                    new Dictionary(
                        "width", "100%"
                        ))
                ;

            _info = jQuery.FromHtml("<div class='InfoCell'/>")
                .AppendTo(_jar)
                ;

            _name = jQuery.FromHtml("<p class='Name Info'/>")
                .AppendTo(_info);

            _bio = jQuery.FromHtml("<p class='Bio Info'/>")
                .AppendTo(_info);

            return this;
        }

        public void FillFromIdAsync(string userId)
        {
            _photo.Hide();
            new Await()
                .AddFn(HideJar)
                .AddAw(ProfileData.GetUserByIdAw, userId)
                .AddDx(
                    delegate(Await aw)
                    {
                        YoshiUser user = (YoshiUser)aw[PkgBase.ResultRx];
                        if (Script.IsValue(user))
                        {
                            Fill(user);
                        }
                    })
                .AddAw(SlideDownAw, SiteTheme.SlideDown)
                .AddDx(delegate { _photo.FadeIn(250); })
                .AddFn(EmailBubbles.ScrollActiveToView)
                .Commit();
        }

        public override EmailBase Fill(object user)
        {
            YoshiUser u = (YoshiUser)user;
            _image.Attribute("src", u.photo);
            _name.Html(u.name);
            _bio.Html(u.bio);
            return this;
        }

    }
}
